<?php
	include 'php/config.php';
	include 'php/scripts/utils.php';
	session_start();		
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>Open Badges - Elenco Badges</title>
<link href="style_no_right.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/cupertino/jquery-ui.css" />
<style type="text/css">
.inside {float:left; padding:25px;}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="js/utils.js"></script>
<script src="js/login.js"></script>
<script>
$(function() {
	$( "#allBadges" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#allBadges" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#categories" ).accordion({active: false});
		$( "#issuers" ).accordion({active: false});
	});

	$( "#categories" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#categories" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#allBadges" ).accordion({active: false});
		$( "#issuers" ).accordion({active: false});
	});

	$( "#issuers" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#issuers" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#categories" ).accordion({active: false});
		$( "#allBadges" ).accordion({active: false});
	});

	$( ".badgeInfo" ).dialog({autoOpen: false, resizable: false, modal: true});

	$( "#loginDiv" ).dialog({autoOpen: false, resizable: false, modal: true});
	$('a#menuLogin').click(function(evt) {
		evt.preventDefault();
		$( "#message" ).text("");
		$( "#userMsg" ).text("");
		$( "#pswMsg" ).text("");
		$( "#loginDiv" ).dialog("open");
	});

	$( "span[id$='badge']" ).each(function() {
		var badgeToCat = $( this ).clone();
		var badgeToIssuer = $( this ).clone();
		var className = $( this ).attr('class');
		var array = className.split('#');
		if ($( '#noCat' + array[0] ).length)
			$( '#noCat' + array[0] ).remove();
		$( '#cat' + array[0] ).append(badgeToCat);
		if ($( '#noIssuer' + array[1] ).length)
			$( '#noIssuer' + array[1] ).remove();
		$( '#issuer' + array[1] ).append(badgeToIssuer);
	});
});

function showInfo(id) {
	$('#'+id).dialog( "open" );
}
</script>
</head>
<body>
	<div id="topPan"><img src="images/logo.gif" alt="Education Zone" width="245" height="37" border="0"  class="logo" title="Education Zone"/>
	  <p>Open Badges</p>
	  <div id="topContactPan"> </div>
	  <div id="topMenuPan">
	    <div id="topMenuLeftPan"></div>
	    <div id="topMenuMiddlePan">
	      <ul>
	        <li><a href="index.php">Home</a></li>
	        <li class="home">Badges</a></li>
	        <li><a href="displayer_page.php">Displayer</a></li>
	        <li><a href="universities.php">Università</a></li>
	        <li><a href="refs.php">Riferimenti</a></li>
	        <li><a href="contacts.php">Contatti</a></li>
	                <?php
				if (!$_SESSION['id_utente']) {
		    	?>
			<li class="contact"><a id="menuLogin" class="contact" href="login.php">Login</a></li>
			<?php
				} else {
			?>
			<li class="contact"><a class="contact" href="logout.php">Logout</a></li>
			<?php
				}
			?>
	      </ul>
	    </div>
	    <div id="topMenuRightPan"></div>
	  </div>
	</div>
	<div id="bodyPan">
		<div id="bodyLeftPan">
			<h2><span>Open Badges - Elenco Badges</span></h2>
			<p></p>
	<?php
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "</p>";
		} else {
			$mysqli->set_charset("utf8");
			$query = $BADGES_QUERY;
			$result = $mysqli->query($query);
			if ($result) {
				if ($result->num_rows) {
					$categoriesQuery = "SELECT `idCategoria`, `nome` FROM `categoria`";
					$categories = $mysqli->query($categoriesQuery);
					$issuersQuery = "SELECT `issuerId`, `nome` FROM `issuer`";
					$issuers = $mysqli->query($issuersQuery);
					$sharedBadgesQuery = "SELECT `badgeId` FROM `badge` WHERE `condivisa`=TRUE";
					$sharedBadges = $mysqli->query($sharedBadgesQuery);
					if (!$categories || !$issuers || !$sharedBadges) {
						echo "<p>Failed to run select query</p>";
					} else {
						echo '<div id="allBadges">';
						echo '<h3>Tutte le badges</h3><div>';
						while ($row = $result->fetch_assoc()) {
							echo generateBadgeHTML($row);
						}
						echo '</div></div>';
						echo '<p></p><h2><span>Per categoria</span></h2><p></p>';
						echo '<div id="categories">';
						while ($category = $categories->fetch_assoc()) {
							echo '<h3>' . $category['nome'] . '</h3>';
							echo '<div id="cat' . $category['idCategoria'] . '">';
							echo '<p id="noCat' . $category['idCategoria'] . '">Non ci sono badges appartenenti a questa categoria</p>';
							echo '</div>';
						}
						echo '</div>';
						echo '<p></p><h2><span>Per issuer</span></h2><p></p>';
						echo '<div id="issuers">';
						if ($sharedBadges->num_rows) {
							echo '<h3>Badge condivise</h3>';
							echo '<div id="issuer0">';
							echo '<p id="noIssuer0">Non ci sono badges rilasciate da tutte le università</p>';
							echo '</div>';
						}
						while ($issuer = $issuers->fetch_assoc()) {
							echo '<h3>' . $issuer['nome'] . '</h3>';
							echo '<div id="issuer' . $issuer['issuerId'] . '">';
							echo '<p id="noIssuer' . $issuer['issuerId'] . '">Non ci sono badges rilasciate solo da questo issuer</p>';
							echo '</div>';
						}
						echo '</div>';
					}
					mysqli_free_result($categories);
					mysqli_free_result($issuers);
					mysqli_free_result($sharedBadges);
				} else {
					echo "<p>Non ci sono badges</p>";
				}				
				
			} else {
				echo "<p>Failed to run query '" . $query . "': (" . $mysqli->errno . ") " .
					$mysqli->error . "</p>";
			}
			
			mysqli_free_result($result);
			$mysqli->close();
		}
	?>
	<div id="loginDiv">
		<h2 id="login"><span>Log</span>in</h2>
		<p id="message" class="errorMsg"></p>
		<form id="loginForm">
		<p class="boldtext">Username</p>
		<p><input id="username" name="username" type="text"/></p>
		<p id="userMsg" class="errorMsg"></p>
		<p class="boldtext">Password</p>
		<p><input id="password" name="password" type="password"/></p>
		<p id="pswMsg" class="errorMsg"></p>
		<p><input type="button" value="Login" onclick="validateLoginForm()"/></p>
		</form>
	</div>
		</div>
	</div>
	<div id="footermainPan">
	  <div id="footerPan">
	    <ul>
	      <li><a href="index.php">Home</a>| </li>
	      <li><a href="badges_page.php">Badges</a>| </li>
	      <li><a href="displayer_page.php">Displayer</a>| </li>
      	      <?php if ($_SESSION['id_utente'] && !$_SESSION['admin']) { ?><li><a href="personal_page.php">Pagina personale</a>| </li> <?php } ?>
	      <li><a href="universities.php">Università</a>| </li>
	      <li><a href="refs.php">Riferimenti</a>| </li>
	      <li><a href="contacts.php">Contatti</a></li>
	    </ul>
	    <p class="copyright">©education zone. All right reserved.</p>
	    <ul class="templateworld">
	      <li>design by:</li>
	      <li><a href="http://www.templateworld.com" target="_blank">Template World</a></li>
	    </ul>
	    <div id="footerPanhtml"><a href="http://validator.w3.org/check?uri=referer" target="_blank">HTML</a></div>
	    <div id="footerPancss"><a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank">css</a></div>
	  </div>
	</div>
</body>
</html>
